# File:     JOP_RR1_sentiment.R
# Purpose:  This script measures the sentiment by hearing (RR comment 5)
# Input:    /Data/finalData.RData
# Output:   
# Author:   JB


rm(list = ls())
require(tidyverse)
require(ggridges)

setwd('C:/Users/Jimbo/Dropbox/FED/FED/Paper/JOP/RR1_replication/')

load('./data/finalData.RData')

pdf('./output/figures/sentimentHearing.pdf',width = 8,height = 5)
utterance_level %>%
  select(docID,yellenTime,date,chamber,matches('SENT_'),-matches('comb|LIKELY|UNSUB|INCOH|_lag')) %>%
  group_by(docID,date,yellenTime,chamber) %>%
  mutate(n = n()) %>%
  summarise_all(mean) %>%
  ungroup() %>%
  mutate(yellenTime = ifelse(date < as.Date('2014-01-01'),'Pre',
                             ifelse(yellenTime,'Yellen','Post'))) %>%
  ungroup() %>%
  # filter(chamber == 'House') %>%
  pivot_longer(cols = starts_with("SENT_")) %>%
  drop_na(value) %>%
  mutate(name = gsub('SENT','',gsub('_',' ',name))) %>%
  ggplot(aes(x = date,y = value,group = yellenTime,size = n)) + 
  geom_point(shape = 21) + 
  geom_smooth(show.legend = F,method = 'lm',se = F,formula = 'y ~ poly(x,1)') + 
  annotate(geom = 'rect',xmin = as.Date('2014-01-01'),xmax = as.Date('2018-01-01'),
           ymin = -Inf,ymax = Inf,
           alpha = .2,fill = 'grey50') +
  geom_vline(xintercept = as.Date(c('2014-01-01','2018-01-01'))) + 
  annotate(geom = 'text',x = as.Date('2016-01-01'),y = Inf,label = 'Yellen',
           vjust = 1) + 
  theme_bw() + 
  theme(axis.text = element_text(size = 7),legend.position = 'bottom') + 
  facet_wrap(~name,scales = 'free') + 
  labs(x = 'Date',y = 'Predicted proportion')
dev.off()